package com.micangcaihang.micang.entity;

import com.micangcaihang.common.utils.SPUtils;
import com.micangcaihang.micang.AppConstant;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/31
 *     update : 2017/07/31
 *     desc   : 保存用户信息单例模式
 *     version: 1.0
 * </pre>
 */

public class UserInfoSp {
    private SPUtils mSPUtils;
    private static UserInfoSp sSPUserInfo = null;
    private boolean isLogin;//是否已经登录
    private boolean isFirstOpen = true;//是否第一次打开
    /**
     * userid : 96
     * username : zhaoyangping
     * photo :
     * token : 2017080216441196#eb16d0b2d7564ca661742c0701831c33
     * realname : 赵阳平
     * department_id : 19   风控18   调评19
     * department_name : 运营风控中心调查评估部
     * position_id : 6
     * position_name : 调查评估专员
     */

    private int userid;//用户编号
    private String username;//用户名
    private String photo;//头像
    private String token;//登录token
    private String realname;//姓名
    private int department_id;//部门编号
    private String department_name; //部门名称
    private int position_id;//职位编号
    private String position_name;//职位名称

    // 实现了线程安全，又避免了同步带来的性能影响。
    public static UserInfoSp getInstance() {
        if (sSPUserInfo == null) {
            synchronized (UserInfoSp.class) {
                if (sSPUserInfo == null) {
                    sSPUserInfo = new UserInfoSp();
                }
            }
        }
        return sSPUserInfo;
    }

    private UserInfoSp() {
        super();
        mSPUtils = SPUtils.getInstance("com.micangcaihang.micang");
    }

    public boolean isLogin() {
        isLogin = mSPUtils.getBoolean(AppConstant.IS_LOGIN);
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
        mSPUtils.put(AppConstant.IS_LOGIN, login);
    }

    public boolean isFirstOpen() {
        isFirstOpen = mSPUtils.getBoolean(AppConstant.IS_FIRST_OPEN, true);
        return isFirstOpen;
    }

    public void setFirstOpen(boolean firstOpen) {
        isFirstOpen = firstOpen;
        mSPUtils.put(AppConstant.IS_FIRST_OPEN, firstOpen);
    }

    public String getUserid() {
        userid = mSPUtils.getInt(AppConstant.USERID);
        return userid == 0 ? "" : String.valueOf(userid);
    }

    public void setUserid(int userid) {
        this.userid = userid;
        mSPUtils.put(AppConstant.USERID, userid);
    }

    public String getUsername() {
        username = mSPUtils.getString(AppConstant.USERNAME, "");
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
        mSPUtils.put(AppConstant.USERNAME, username);
    }

    public String getPhoto() {
        photo = mSPUtils.getString(AppConstant.PHOTO, "");
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
        mSPUtils.put(AppConstant.PHOTO, photo);
    }

    public String getToken() {
        token = mSPUtils.getString(AppConstant.TOKEN, "");
        return token == null ? "" : token;
    }

    public void setToken(String token) {
        this.token = token;
        mSPUtils.put(AppConstant.TOKEN, token);
    }

    public String getRealname() {
        realname = mSPUtils.getString(AppConstant.REALNAME, "");
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
        mSPUtils.put(AppConstant.REALNAME, realname);
    }

    public int getDepartment_id() {
        department_id = mSPUtils.getInt(AppConstant.DEPARTMENT_ID);
        return department_id;
    }

    public void setDepartment_id(int department_id) {
        this.department_id = department_id;
        mSPUtils.put(AppConstant.DEPARTMENT_ID, department_id);
    }

    public String getDepartment_name() {
        department_name = mSPUtils.getString(AppConstant.DEPARTMENT_NAME, "");
        return department_name;
    }

    public void setDepartment_name(String department_name) {
        this.department_name = department_name;
        mSPUtils.put(AppConstant.DEPARTMENT_NAME, department_name);
    }

    public int getPosition_id() {
        position_id = mSPUtils.getInt(AppConstant.POSITION_ID);
        return position_id;
    }

    public void setPosition_id(int position_id) {
        this.position_id = position_id;
        mSPUtils.put(AppConstant.POSITION_ID, position_id);
    }

    public String getPosition_name() {
        position_name = mSPUtils.getString(AppConstant.POSITION_NAME, "");
        return position_name;
    }

    public void setPosition_name(String position_name) {
        this.position_name = position_name;
        mSPUtils.put(AppConstant.POSITION_NAME, position_name);
    }

    /***
     * 读取配置文件，并赋值
     */
    public void getAccess() {
        isLogin();
        isFirstOpen();
        getUserid();
        getUsername();
        getPhoto();
        getToken();
        getRealname();
        getDepartment_id();
        getDepartment_name();
        getPosition_id();
        getPosition_name();
    }

    /***
     * 写入登录信息配置文件
     */
    public void setAccess(UserEntity userEntity) {
        setLogin(true);
        setUserid(userEntity.getUserid());
        setUsername(userEntity.getUsername());
        setPhoto(userEntity.getPhoto());
        setToken(userEntity.getToken());
        setRealname(userEntity.getRealname());
        setDepartment_id(userEntity.getDepartment_id());
        setDepartment_name(userEntity.getDepartment_name());
        setPosition_id(userEntity.getPosition_id());
        setPosition_name(userEntity.getPosition_name());
        getAccess();
    }

    public void clearAccount() {
        mSPUtils.clear();
        setLogin(false);
        setFirstOpen(false);
    }
}
